/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "TypeMeta.h"

#include "MetaReference.h"

#include "../common/BigEndianUnsignedShort.h"
#include "../common/U32UTF8.h"
#include "../common/U8ASCII.h"

type_name meta_reference_name = { "meta.reference" };

int meta_reference_bind( type_element * element, type_definition * definition )
{
	int rc;
	
	rc = type_object_bind( &element->object, definition->library, &meta_reference_name );
	if ( rc != TYPE_OK ) return rc;

	rc = type_element_bind( element, definition );
	if ( rc != TYPE_OK ) return rc;
	
	return TYPE_OK;
}

void meta_reference_init( meta_reference * mr, int type, char * desc )
{
	type_element_init( &mr->expression.element, &meta_reference_bind );
	meta_expression_init( &(mr->expression), &meta_reference_do_read, &meta_reference_do_write );
	
	mr->type = type;
	mr->description = desc;
}

void meta_reference_set_type( meta_reference * mr, int type )
{
	mr->type = type;
}

int meta_reference_get_type( meta_reference * mr )
{
	return mr->type;
}

char * meta_reference_get_description( meta_reference * mr )
{
	return mr->description;
}

void * meta_reference_construct( type_list * list, type_map * map )
{
	type_item * item = type_list_head( list );
	int type = type_item_get_u16( item );
	
	/* check the id is valid and map it to our library. */
	reference_type_map * map_core = (reference_type_map *) map;
	type_map * ref_map = reference_type_map_get_reference( map_core );
	
	if ( type_map_is_valid_id( ref_map, type ) )
	{
		type = type_map_get_library_id( ref_map, type );
	}
	else
	{
		/* TODO this is an error. */
	}
	
	item = type_item_next( item );
	/* FIX ME A BIT OF A HACK. BAD META DICTIONARY */
	char * desc = ((type_name *) type_item_get_ptr( item ))->name;
	meta_reference * mr = (meta_reference *) type_malloc( sizeof( meta_reference ) );
	meta_reference_init( mr, type, desc );
	return mr;
}

int meta_reference_read( type_input_stream * in, type_element * element )
{
	return type_reader_auto( in, element, meta_reference_construct );
}

int meta_reference_write( type_output_stream * out, void * o, type_element * element )
{
	int rc;
	meta_reference * mr = (meta_reference *) o;
	reference_type_map * map = (reference_type_map * ) type_output_type_map( out );
	type_map * ref_map = reference_type_map_get_reference( map );
	
	int id = type_map_get_mapped_id( ref_map, mr->type );
	if ( id <= NOTYPE ) return id;
	
	rc = type_output_write_u16( out, id );
	if ( rc != TYPE_OK ) return rc;
	
	/* FIXME u8ascii is not registered at this time */
	rc = u8ascii_write( out, mr->description, NULL );		
	/*rc = type_output_write_named_object( out, &u8ascii_name, mr->description );*/
	if ( rc != TYPE_OK ) return rc;

	return TYPE_OK;	
}

int meta_reference_do_read( type_input_stream * in, type_element * element )
{
	meta_reference * mr = (meta_reference *) element;
	return type_input_read_object( in, type_map_get_mapped_id( type_input_type_map( in ), mr->type ));
}

int meta_reference_do_write( type_output_stream * out, void * o, type_element * element )
{
	meta_reference * mr = (meta_reference *) element;
	return type_output_write_object( out, type_map_get_mapped_id( type_output_type_map( out ), mr->type ), o );
}


